{% macro construct(property, source) %}
{% set inner_property = property.inner_property %}
{% import "property_templates/" + inner_property.template as inner_template %}
{% if inner_template.construct %}
{% set inner_source = inner_property.python_name + "_data" %}
{% if property.required %}
{{ property.python_name }} = []
_{{ property.python_name }} = {{ source }}
for {{ inner_source }} in (_{{ property.python_name }}):
    {{ inner_template.construct(inner_property, inner_source) | indent(4) }}
    {{ property.python_name }}.append({{ inner_property.python_name }})
{% else %}
_{{ property.python_name }} = {{ source }}
{{ property.python_name }}: {{ property.get_type_string() }} = UNSET
if _{{ property.python_name }} is not UNSET:
    {{ property.python_name }} = []
    for {{ inner_source }} in _{{ property.python_name }}:
        {{ inner_template.construct(inner_property, inner_source) | indent(8) }}
        {{ property.python_name }}.append({{ inner_property.python_name }})
{% endif %}
{% else %}
{{ property.python_name }} = cast({{ property.get_type_string(no_optional=True) }}, {{ source }})
{% endif %}
{% endmacro %}

{% macro _transform(property, source, destination, transform_method) %}
{% set inner_property = property.inner_property %}
{% import "property_templates/" + inner_property.template as inner_template %}
{% if inner_template.transform %}
{% set inner_source = inner_property.python_name + "_data" %}
{{ destination }} = []
for {{ inner_source }} in {{ source }}:
    {{ inner_template.transform(inner_property, inner_source, inner_property.python_name, transform_method) | indent(4) }}
    {{ destination }}.append({{ inner_property.python_name }})
{% else %}
{{ destination }} = {{ source }}
{% endif %}
{% endmacro %}

{% macro check_type_for_construct(property, source) %}isinstance({{ source }}, list){% endmacro %}

{% macro transform(property, source, destination, declare_type=True, skip_unset=False) %}
{% set inner_property = property.inner_property %}
{% set type_string = property.get_type_string(json=True) %}
{% if property.required %}
{{ _transform(property, source, destination, "to_dict") }}
{% else %}
{% if not skip_unset %}{{ destination }}{% if declare_type %}: {{ type_string }}{% endif %} = UNSET{% endif +%}
if not isinstance({{ source }}, Unset):
    {{ _transform(property, source, destination, "to_dict") | indent(4)}}
{% endif %}
{% endmacro %}

{% macro multipart(property, source, destination) %}
{% set inner_property = property.inner_property %}
{% import "property_templates/" + inner_property.template as inner_template %}
{% set inner_source = inner_property.python_name + "_element" %}
for {{ inner_source }} in {{ source }}:
    {{ inner_template.multipart(inner_property, inner_source, destination) | indent(4) }}
{% endmacro %}
